package com.link.base.base.wechat.controller;

import com.link.base.base.wechat.model.WxCardSign;
import com.link.base.base.wechat.service.WxCardSignService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卡券签名
 *
 * @author KuangRuifeng
 * @date 2018年12月15日
 */
@Controller
@RequestMapping("/link/WxCardSign")
public class WxCardSignController {

    @Resource
    private WxCardSignService wxCardSignService;

    /**
     * 获取领取微信卡券需要的扩展字段
     *
     * @author KuangRuifeng
     * @date 2018年12月11日
     */
    @RequestMapping(value = "/getCardExt")
    public Map<String, Object> getCardExt(@RequestBody WxCardSign wxCardSign, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (wxCardSign.getSignList().size() == 0) {
                throw new ServiceException("WECHAT-001");
            }
            List<WxCardSign> list = wxCardSignService.getCardExt(wxCardSign);
            result.put("result", list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }

    /**
     * 对添加微信卡券后返回的加密code进行解密
     *
     * @author KuangRuifeng
     * @date 2018年12月11日
     */
    @RequestMapping(value = "/decryptCardCode")
    public Map<String, Object> decryptCardCode(@RequestBody WxCardSign wxCardSign, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (wxCardSign.getSignList().size() == 0) {
                throw new ServiceException("WECHAT-001");
            }
            List<WxCardSign> list = wxCardSignService.decryptCardCode(wxCardSign);
            result.put("result", list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }

    /**
     * 查询appid
     * 
     * @author huanglongjian
     * @date 2019-11-04 14:21
     */
    @RequestMapping(value = "/getAppid")
    public Map<String, Object> getAppid(@RequestBody Long wxPlatformId, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            String appId = wxCardSignService.getAppId(wxPlatformId);
            result.put("result", appId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }

    /**
     * 获取开卡组件参数
     *
     * @author KuangRuifeng
     * @date 2019年4月30日
     */
    @RequestMapping(value = "/getExtraData")
    public Map<String, Object> getExtraData(@RequestBody WxCardSign wxCardSign, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Map<String, String> map = wxCardSignService.getExtraData(wxCardSign);
            result.put("result", map);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }
}
